package org.lvhengchang.schoolmanager.servlet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import org.lvhengchang.schoolmanager.bean.Dict;
import org.lvhengchang.schoolmanager.service.DictService;
import org.lvhengchang.schoolmanager.service.impl.DictServiceImpl;

/**
 * Servlet implementation class InitServlet
 */
@WebServlet(value = "/init", name = "startUpInit", loadOnStartup = 1)
public class InitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DictService dictService = new DictServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InitServlet() {
    }
    
    @Override
    public void init() throws ServletException {
    	System.out.println("开始加载字典数据");
    	Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>(); 
    	List<Dict> dicts;
		try {
			dicts = dictService.selectByAll();
		
    	for (Dict dict : dicts) {
			if (map.containsKey(dict.getDict_type_id())) {
				Map<String, String> value = map.get(dict.getDict_type_id());
				value.put(dict.getDict_code_id(), dict.getDict_code_name());
				map.put(dict.getDict_type_id(), value);
			} else {
				Map<String, String> value = new HashMap<String, String>();
				value.put(dict.getDict_code_id(), dict.getDict_code_name());
				map.put(dict.getDict_type_id(),value);
			}
		}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	getServletContext().setAttribute("dict", map);
    	System.out.println("字典数据加载完成");
    }
    
}
